package com.dev.module_zqc_sleep.mvi.ui

import android.content.Intent
import android.content.IntentFilter
import android.icu.text.SimpleDateFormat
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.dev.module_ui_composable.components.noRippleClickable
import com.dev.module_ui_composable.utils.ToastUtils
import com.dev.module_ui_composable.view.ad.BannerAdView
import com.dev.module_zqc_sleep.R
import com.dev.module_zqc_sleep.broadcast.TimeTickReceiver
import com.dev.module_zqc_sleep.mvi.vm.AlarmViewModel
import java.util.Date
import java.util.Locale

@Composable
fun AlarmClockPage(
    alarmViewModel: AlarmViewModel,
    onNavigateSetting: () -> Unit,
    onStartAlarm: (Boolean, String) -> Unit
) {
    val context = LocalContext.current
    val isAlarmEnabled by alarmViewModel.isAlarmEnabled.collectAsStateWithLifecycle(false)
    val selectedTime by alarmViewModel.selectedTime.collectAsStateWithLifecycle("")
    var currentTime by remember { mutableStateOf("") }
    val formatter = remember { SimpleDateFormat("HH:mm", Locale.getDefault()) }

    LaunchedEffect(selectedTime, currentTime) {
        alarmViewModel.readAlarmEnabled(context)
        alarmViewModel.loadSelectedTime(context)
    }

    DisposableEffect(context) {
        currentTime = formatter.format(Date())

        val receiver = TimeTickReceiver {
            currentTime = formatter.format(Date())
        }
        val filter = IntentFilter(Intent.ACTION_TIME_TICK)
        context.registerReceiver(receiver, filter)

        onDispose {
            context.unregisterReceiver(receiver)
        }
    }

    Scaffold(
        modifier = Modifier.fillMaxWidth(),
        contentWindowInsets = WindowInsets(0, 0, 0, 0),
        containerColor = Color.Black
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(innerPadding)
        ) {
            AlarmClockContent(
                currentTime = currentTime,
                selectedTime = selectedTime,
                isAlarmEnabled = isAlarmEnabled,
                onStartAlarm = {
                    if (selectedTime.isEmpty()) {
                        ToastUtils.showShortToast(context, "请设置起床时间")
                    } else {
                        onStartAlarm(isAlarmEnabled, selectedTime)
                    }
                },
                onNavigateSetting = {
                    if (!isAlarmEnabled) {
                        onNavigateSetting()
                    } else {
                        ToastUtils.showShortToast(context, "请先关闭闹钟")
                    }
                }
            )
        }
    }
}

@Composable
private fun AlarmClockContent(
    currentTime: String,
    selectedTime: String,
    isAlarmEnabled: Boolean,
    onStartAlarm: () -> Unit,
    onNavigateSetting: () -> Unit
) {
    Surface(
        modifier = Modifier
            .fillMaxWidth()
            .padding(top = 32.dp),
        color = Color.Black,
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 25.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            Box(
                modifier = Modifier
                    .wrapContentSize()
                    .noRippleClickable { onNavigateSetting() },
                contentAlignment = Alignment.Center
            ) {
                Image(
                    painter = painterResource(R.mipmap.img_sleep_1),
                    contentDescription = null,
                    modifier = Modifier.wrapContentSize(),
                    contentScale = ContentScale.FillBounds
                )

                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.spacedBy(12.dp, Alignment.CenterVertically)
                ) {
                    Text(
                        text = selectedTime.ifEmpty { "00:00" },
                        fontSize = 28.sp,
                        color = Color(0xFFFFFFFF)
                    )
                    if (isAlarmEnabled) {
                        Row(
                            verticalAlignment = Alignment.CenterVertically,
                            horizontalArrangement = Arrangement.spacedBy(
                                8.dp,
                                Alignment.CenterHorizontally
                            )
                        ) {
                            Image(
                                painter = painterResource(R.mipmap.m_alarm_ic4),
                                contentDescription = null,
                                modifier = Modifier.wrapContentSize(),
                                contentScale = ContentScale.FillBounds
                            )

                            Text(
                                text = "闹钟已开",
                                color = Color(0xFFFFFFFF)
                            )
                        }
                    }
                }
            }

//            FilledTonalButton(
//                onClick = { onStartAlarm() },
//                colors = ButtonDefaults.filledTonalButtonColors(
//                    containerColor = if (isAlarmEnabled) Color.White else Color(0xFF706CF6),
//                    contentColor = if (isAlarmEnabled) Color.Black else Color.White
//                ),
//                border = if (isAlarmEnabled) BorderStroke(width = 1.dp, color = Color(0xFF706CF6)) else null
//            ) {
//                Text(
//                    text = if (isAlarmEnabled) "结束小憩" else "开始就寝",
//                    fontSize = 18.sp
//                )
//            }
            Image(
                painter = painterResource(if (isAlarmEnabled) R.mipmap.a_gj_12 else R.mipmap.a_gj_10),
                contentDescription = null,
                modifier = Modifier.clickable { onStartAlarm() })

            Row(
                modifier = Modifier.fillMaxWidth().padding(top = 25.dp),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.spacedBy(30.dp, Alignment.CenterHorizontally)
            ) {
                Surface(
                    onClick = { onNavigateSetting() },
                    color = Color.Black,
//                    shape = RoundedCornerShape(25.dp)
                ) {
//                    Column(
//                        modifier = Modifier.fillMaxSize(),
//                        horizontalAlignment = Alignment.CenterHorizontally,
//                        verticalArrangement = Arrangement.SpaceEvenly
//                    ) {
                    Box {
                        Image(painter = painterResource(R.mipmap.a_gj_9), contentDescription = null, modifier = Modifier.size(130.dp), contentScale = ContentScale.FillBounds)
                        Text(
                            modifier = Modifier.align(Alignment.TopCenter).padding(top = 43.dp),
                            text = currentTime,
                            fontSize = 18.sp,
                            fontWeight = FontWeight.Bold,
                            color = Color.White
                        )
                    }

//                        Row(
//                            modifier = Modifier
//                                .fillMaxWidth()
//                                .padding(horizontal = 5.dp),
//                            verticalAlignment = Alignment.CenterVertically,
//                            horizontalArrangement = Arrangement.spacedBy(
//                                8.dp,
//                                Alignment.CenterHorizontally
//                            )
//                        ) {
//                            Image(
//                                painter = painterResource(R.mipmap.m_alarm_ic1),
//                                contentDescription = null,
//                                modifier = Modifier.wrapContentSize(),
//                                contentScale = ContentScale.FillBounds
//                            )
//
//                            Text(
//                                text = "就寝目标",
//                                color = Color(0xFFA1E4F0)
//                            )
//                        }
//                    }
                }

                Surface(
                    onClick = { onNavigateSetting() },
                    color = Color.Black,
//                    shape = RoundedCornerShape(25.dp)
                ) {
                    Box {
                        Image(painter = painterResource(R.mipmap.a_gj_8), contentDescription = null, modifier = Modifier.size(130.dp), contentScale = ContentScale.FillBounds)
                        Text(
                            modifier = Modifier.align(Alignment.TopCenter).padding(top = 43.dp),
                            text = selectedTime.ifEmpty { "00:00" },
                            fontSize = 18.sp,
                            fontWeight = FontWeight.Bold,
                            color = Color.White
                        )
                    }
//                    Column(
//                        modifier = Modifier.fillMaxSize(),
//                        horizontalAlignment = Alignment.CenterHorizontally,
//                        verticalArrangement = Arrangement.SpaceEvenly
//                    ) {
//                        Text(
//                            text = selectedTime.ifEmpty { "00:00" },
//                            fontSize = 20.sp,
//                            fontWeight = FontWeight.Bold
//                        )
//
//                        Row(
//                            modifier = Modifier
//                                .fillMaxWidth()
//                                .padding(horizontal = 5.dp),
//                            verticalAlignment = Alignment.CenterVertically,
//                            horizontalArrangement = Arrangement.spacedBy(
//                                8.dp,
//                                Alignment.CenterHorizontally
//                            )
//                        ) {
//                            Image(
//                                painter = painterResource(R.mipmap.m_alarm_ic2),
//                                contentDescription = null,
//                                modifier = Modifier.wrapContentSize(),
//                                contentScale = ContentScale.FillBounds
//                            )
//
//                            Text(
//                                text = "起床目标",
//                                color = Color(0xFFA1E4F0)
//                            )
//                        }
//                    }
                }
            }
        }
    }
}